package cn.pku.sirx.snad.ads.wordsegmentation;

import java.io.Reader;
import java.io.StringReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.wltea.analyzer.IKSegmentation;
import org.wltea.analyzer.Lexeme;

/**
 * Implementation using IKAnalyzer
 * 
 * @author WangXiao
 */
public class IKAnalyzerImpl implements IWordSegmentation {

	@Override
	public List<String> segmentWords(String sentences)
			throws WordSegmentationException {
		if (sentences == null) {
			return Collections.emptyList();
		}

		List<String> result = new LinkedList<String>();
		Reader reader = new StringReader(sentences);
		IKSegmentation segmentation = new IKSegmentation(reader, true);
		Lexeme lexeme;
		try {
			while ((lexeme = segmentation.next()) != null) {
				if (!filter(lexeme)) {
					result.add(lexeme.getLexemeText());
				}
			}
		} catch (Exception e) {
			throw new WordSegmentationException(e);
		}
		return result;
	}

	/*
	 * filter not important words
	 */
	private boolean filter(Lexeme lexeme) {
		int type = lexeme.getLexemeType();
		if (type != Lexeme.TYPE_CJK_NORMAL && type != Lexeme.TYPE_LETTER) {
			return true;
		}
		return false;
	}

}
